﻿using System.Web;
using System.Web.SessionState;

namespace SharePointMvc
{
    public class RoutingHttpHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!RoutingManager.UseDefaultHttpHandler(context))
            {
                RoutingManager.AddRouteToCache(context);
                try
                {
                    context.Server.Transfer(RoutingManager.GetRouteConfigurationSection().RoutingRedirectionPage, true);
                }
                catch
                {
                    // if the a unhandled error has occured rethrow it.
                    // otherwise we want the actual error that has occurred to be processed.
                    if (context.AllErrors == null || context.AllErrors.Length == 0)
                    {
                        throw;
                    }
                }
            }
            else
            {
                RoutingManager.ProcessStandardRequest(context);
            }
        }

        public bool IsReusable
        {
            get { return true; }
        }
    }
}